// src/pages/catering/goodsList/cartList/cartList.js
import getIn from '../../../../../yali-sdk/utils/getIn';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    color: {
      type: String,
      value: '#ee2b47',
    },
    template:{
      type: Number,
      value: 0,
    },
    carArray: {
      type: Array,
      value: [],
    },
    fold: {
      type: Boolean,
      value: false,
      observer(newVal, oldVal) {
        if (newVal) {
          this.showModal();
        }
      },
    },
    detailShow:{
      type: Boolean,
      value: false,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**弹出优惠券弹窗**/
    showModal() {
      this.timers && clearTimeout(this.timers);
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'linear',
        delay: 0,
      });
      this.animation = animation;
      const window_height = getIn(getApp(), ['globalData', 'window_height']);
      animation.translateY(window_height - 52).step();
      this.setData({ animationData: animation.export() });
      this.times = setTimeout(() => {
        animation.translateY(- 52).step();
        this.setData({
          animationData: animation.export(),
        });
        this.triggerEvent('hideCartCallBack', { fold: true });
      }, 200);
    },
    // 取消弹出框
    hideModal() {
      this.timers && clearTimeout(this.timers);
      const animation = wx.createAnimation({
        duration: 200,
        timingFunction: 'linear',
        delay: 0,
      });
      this.animation = animation;
      const window_height = getIn(getApp(), ['globalData', 'window_height']);
      animation.translateY(window_height - 52).step();
      this.setData({ animationData: animation.export() });
      this.times = setTimeout(() => {
        animation.translateY(-52).step();
        this.setData({
          animationData: animation.export(),
        });
        this.triggerEvent('hideCartCallBack', { fold: false })
      }, 200);
    },
    // 修改购物车数据
    modifiesShopCart(e) {
      const num = getIn(e.currentTarget.dataset, ['num']);
      const oldnum = getIn(e.currentTarget.dataset, ['oldnum'])
      const new_attr = getIn(e.currentTarget.dataset, ['group_attr']);
      const group_attr = new_attr != "" ? new_attr : undefined;
      this.triggerEvent('modifiesCart', { id: e.currentTarget.id, num, group_attr, oldnum });
    },
    // 清空购物车
    clear() {
      this.triggerEvent('clearCart');
    },
    // 酒店查看明细
    toDetail(e) {
      const hotelRoomIfo = getIn(e.currentTarget.dataset, ['item']);
      this.hideModal();
      this.triggerEvent('hideCartCallBack', { detailShow: true });
      this.setData({ hotelRoomIfo });
    },
    // 酒店退出明细
    signout() {
      this.showModal();
      this.triggerEvent('hideCartCallBack', { detailShow: false });
    }
  }
})
